package study.web.service.socket.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

/**  
* @Name: WeatherClient
* @Description: 天气查询客户端（socket实现）
* @Author: 李超
* @version 1.0
* @Create Date: 2017-01-06
*/
public class WeatherClient {
	public static void main(String[] args) throws UnknownHostException, IOException {
		while(true) {
			//和socket服务建立一个socket连接
			Socket socket = new Socket("127.0.0.1", 12345) ;
			DataInputStream dataInputStream = null ;
			DataOutputStream dataOutputStream = null ;
			try {
				//准备发送数据的流
				dataOutputStream = new DataOutputStream(socket.getOutputStream()) ;
				//准备接受数据的流
				dataInputStream = new DataInputStream(socket.getInputStream()) ;
				//向服务端发送数据
				String cityName = "郑州" ;
				System.out.println("to server ... " + cityName);
				dataOutputStream.writeUTF(cityName) ;
				//接受服务端响应的数据
				String result = dataInputStream.readUTF() ;
				System.out.println("from server ... " + result);
			} catch (Exception e) {
				e.printStackTrace() ;
			} finally {
				//释放资源
				if(dataInputStream != null) {
					dataInputStream.close() ;
				}
				if(dataOutputStream != null) {
					dataOutputStream.close() ;
				}
				//关闭链接
				if(socket != null) {
					socket.close() ;
				}
			}
		}
	}
	
}
